/* Copyright (c) 2009-2010, Sean A.O. Harney <sharney3@gmail.com> */
// for newline seperated dictionary files.
//  eg /usr/share/dict/cracklib-small

#include "omf.h"

FILE *_dictfile_fp = NULL;

bool dictfile_launch(const char *fpath)
{
    _dictfile_fp = fopen(fpath, "r");
    if (_dictfile_fp == NULL)
      {
	  perror(fpath);
	  return false;
      }
    return true;
}


// returns NULL on error or end of permutations
// returns \0 terminated C string.
char *dictfile_next_permutation(void)
{
#define MAX_STR 32768		// should be enough for now.
    static char str[MAX_STR];	//hard limit for now..
    //TODO: static? does it make it faster, probably not, but don't have to worry about stack allocation problems with large sizes.
    char *p;

    if (fgets(str, MAX_STR, _dictfile_fp) == NULL)
      {
	  perror("fgets()");
	  return NULL;
      }

    p = strchr(str, '\n');
    if (p != NULL)
	*p = '\0';

    return str;
}
